import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'

Vue.use(VueRouter)

const routes = [{
  path:'/',
  redirect:'/login'
},
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path:'/home',
    name:'home',
    redirect:'welcome',
    component:()=>import('../components/home.vue'),
    children:[
      {path:'/welcome',component:()=>import('../components/index/welcome.vue')},
      {path:'/users',component:()=>import('../components/user/users.vue')},
      {path:'/roles',component:()=>import('../components/power/roles.vue')},
      {path:'/rights',component:()=>import('../components/power/rights.vue')},
      {path:'/goods',component:()=>import('../components/goods/goods.vue')},
      {path:'/params',component:()=>import('../components/goods/params.vue')},
      {path:'/categories',component:()=>import('../components/goods/categories.vue')},
      {path:'/goods/addGoods',component:()=>import('../components/goods/addGoods.vue')},
      {path:'/orders',component:()=>import('../components/order/orders.vue')},

    ]
  }

]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  //如果是登录页，则放行
  if(to.path==='/login'){
    return next()
  }
  // 如果未登录，跳转登录
  const userInfo=JSON.parse(sessionStorage.getItem('userInfo'))
  if(!userInfo){
    return next('/login')
  }
  //已登录，则放行
  next()

})

export default router
